package com.sunday.haowushop.ui.main;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sunday.common.base.BaseFragment;
import com.sunday.common.event.EventBus;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.SharePerferenceUtils;
import com.sunday.common.widgets.CircleImageView;
import com.sunday.haowushop.R;
import com.sunday.haowushop.common.BaseApp;
import com.sunday.haowushop.entity.Member;
import com.sunday.haowushop.entity.ProfitAll;
import com.sunday.haowushop.event.UpdateBaseInfo;
import com.sunday.haowushop.event.UpdateProfit;
import com.sunday.haowushop.http.ApiClient;
import com.sunday.haowushop.ui.WebViewActivity;
import com.sunday.haowushop.ui.cart.CartActivity;
import com.sunday.haowushop.ui.mine.AddressListActivity;
import com.sunday.haowushop.ui.mine.AvailableProfitActivity;
import com.sunday.haowushop.ui.mine.HelpCenterActivity;
import com.sunday.haowushop.ui.mine.MessageListActivity;
import com.sunday.haowushop.ui.mine.MyIntegralActivity;
import com.sunday.haowushop.ui.mine.SettingActivity;
import com.sunday.haowushop.ui.mine.TotalProfitActivity;

import com.sunday.haowushop.ui.mine.details.BalanceDetailActivity;

import com.sunday.haowushop.ui.mine.voucher.VoucherListActivity;
import com.sunday.haowushop.ui.mine.withdraw.MyAccountActivity;

import java.math.RoundingMode;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.everything.android.ui.overscroll.OverScrollDecoratorHelper;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.sunday.haowushop.R.id.shop_number;


/**
 * Created by 刘涛 on 2017/4/11.
 */

public class MineFragment extends BaseFragment {


    @Bind(R.id.user_logo)
    CircleImageView userLogo;
    @Bind(R.id.layout_user_logo)
    FrameLayout layoutUserLogo;
    @Bind(R.id.shop_name)
    TextView shopName;
    @Bind(shop_number)
    TextView shopNumber;
    @Bind(R.id.header_layout)
    RelativeLayout headerLayout;
    @Bind(R.id.money)
    TextView money;
    @Bind(R.id.total_profit)
    TextView totalProfit;
    @Bind(R.id.integral_num)
    TextView integralNum;
    @Bind(R.id.my_coupon)
    TextView myCoupon;
    @Bind(R.id.layout2)
    LinearLayout layout2;
    @Bind(R.id.textView3)
    TextView textView3;
    @Bind(R.id.textView4)
    TextView textView4;
    @Bind(R.id.textView5)
    TextView textView5;
    @Bind(R.id.textView6)
    TextView textView6;
    @Bind(R.id.layout3)
    LinearLayout layout3;
    @Bind(R.id.menu_trial)
    TextView menuTrial;
    @Bind(R.id.menu_addr)
    TextView menuAddr;
    @Bind(R.id.menu_about)
    TextView menuAbout;
    @Bind(R.id.scrollView)
    ScrollView scrollView;
    private Member member;


    public static MineFragment newInstance() {
        MineFragment fragment = new MineFragment();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.layout_fragment_mine, container, false);
        ButterKnife.bind(this, rootView);
        ScrollView scrollView = (ScrollView) rootView.findViewById(R.id.scrollView);
        OverScrollDecoratorHelper.setUpOverScroll(scrollView);
        return rootView;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        EventBus.getDefault().register(this);
        member = BaseApp.getInstance().getMember();
        initView();
        getMemberInfo();
    }

    public void onEvent(UpdateProfit updateProfit) {
        getProfits();
    }

    public void onEvent(UpdateBaseInfo updateBaseInfo) {
        initView();
    }

    private void initView() {
        if (!TextUtils.isEmpty(member.getLogo())) {
            Glide.with(mContext)
                    .load(member.getLogo())
                    .error(R.mipmap.ic_logo_default)
                    .into(userLogo);
        }
        shopName.setText(member.getNickName());
        shopNumber.setText(String.valueOf(member.getLevelName()));


    }
    ProfitAll profitAll;
    public void getProfits() {
        Call<ResultDO<ProfitAll>> call = ApiClient.getApiAdapter().getMyProfit(BaseApp.getInstance().getMember().getId());
        call.enqueue(new Callback<ResultDO<ProfitAll>>() {
            @Override
            public void onResponse(Call<ResultDO<ProfitAll>> call, Response<ResultDO<ProfitAll>> response) {
                if (isFinish) {
                    return;
                }
                if (response.body() == null) {
                    return;
                }
                if (response.body().getCode() == 0) {
                    if (response.body().getResult() == null) {
                        return;
                    }
                    profitAll  = response.body().getResult();
                    money.setText(String.format("%s", profitAll.getWithdrawAmount().setScale(2, RoundingMode.HALF_UP)));
                    totalProfit.setText(String.format("%s", profitAll.getTotalProfit().setScale(2, RoundingMode.HALF_UP)));
                    myCoupon.setText("" + profitAll.getVoucherCount());
                    integralNum.setText(""+profitAll.getScore());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<ProfitAll>> call, Throwable t) {

            }
        });
    }


    @OnClick({R.id.left_menu, R.id.right_menu, R.id.right_menu1, R.id.account_layout, R.id.menu_trial, R.id.menu_addr,
            R.id.menu_about, R.id.money, R.id.textView3, R.id.total_profit, R.id.textView4, R.id.integral_num, R.id.textView5,
            R.id.my_coupon, R.id.textView6})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.left_menu:
                intent = new Intent(mContext, MessageListActivity.class);
                startActivity(intent);
                break;
            case R.id.right_menu:
                intent = new Intent(mContext, SettingActivity.class);
                startActivity(intent);
                break;
            case R.id.right_menu1:
                intent = new Intent(mContext, CartActivity.class);
                startActivity(intent);
                break;
            case R.id.account_layout:
                intent = new Intent(mContext, MyAccountActivity.class);
                startActivity(intent);
                break;
            case R.id.menu_trial:
                //试用中心
                break;
            case R.id.menu_addr:
                intent = new Intent(mContext, AddressListActivity.class);
                startActivity(intent);
                break;
            case R.id.menu_about:
                break;
            case R.id.money:
            case R.id.textView3:
                intent = new Intent(mContext, BalanceDetailActivity.class);
                intent.putExtra("money",String.format("%s", profitAll.getWithdrawAmount().setScale(2, RoundingMode.HALF_UP)));
                startActivity(intent);
                break;
            case R.id.total_profit:
            case R.id.textView4:
                intent = new Intent(mContext, TotalProfitActivity.class);
                startActivity(intent);
                break;
            case R.id.integral_num:
            case R.id.textView5:
                //积分
                intent = new Intent(mContext, MyIntegralActivity.class);
                intent.putExtra("score",profitAll.getScore());
                startActivity(intent);
                break;
            case R.id.my_coupon:
            case R.id.textView6:
                intent = new Intent(mContext, VoucherListActivity.class);
                intent.putExtra("type", 1);
                startActivity(intent);
                break;

//            case R.id.apply_cash:
//                intent = new Intent(mContext, WithDrawActivity.class);
//                startActivity(intent);
//                break;
//            case R.id.check_yunbi:
//                intent = new Intent(mContext, VoucherListActivity.class);
//                intent.putExtra("type", 2);
//                startActivity(intent);
//                break;
//            case R.id.check_coupon:
//
//            case R.id.layout_total_profit:
//
//                break;
//            case R.id.layout_wait_profit:
//                //待收收益
//                intent = new Intent(mContext, AvailableProfitActivity.class);
//                startActivity(intent);
//                break;
//            case R.id.layout_user_logo:
//                intent = new Intent(mContext, UserInfoActivity.class);
//                startActivity(intent);
//                break;
//
//            case R.id.txt_money:
//
//
//                break;
//            case R.id.statistic_center:
//                intent = new Intent(mContext, WebViewActivity.class);
//                intent.putExtra("url", String.format("http://admin.zj-yunti.com/DataController/reportList?memberId=%d", member.getId()));
//                intent.putExtra("title", "报表中心");
//                startActivity(intent);
//                break;
//            case R.id.menu_group_order:
//                intent = new Intent(mContext, GroupBuyOrderListActivity.class);
//                startActivity(intent);
//                break;
//            case R.id.help_center:
//                intent = new Intent(mContext, HelpCenterActivity.class);
//                startActivity(intent);
//                break;
//            case R.id.menu_shop_order:
//                intent = new Intent(mContext, OpenShopOrderActivity.class);
//                startActivity(intent);
//                break;

        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    public void getMemberInfo() {
        Call<ResultDO<Member>> call = ApiClient.getApiAdapter().getMemberInfo(BaseApp.getInstance().getMember().getId());
        call.enqueue(new Callback<ResultDO<Member>>() {
            @Override
            public void onResponse(Call<ResultDO<Member>> call, Response<ResultDO<Member>> response) {
                if (isFinish || response.body() == null) {
                    return;
                }
                if (response.body().getCode() == 0) {
                    if (response.body().getResult() != null)
                        member = response.body().getResult();
                    SharePerferenceUtils.getIns(mContext).saveOAuth(member);
                    BaseApp.getInstance().setMember(member);
                    initView();
                    getProfits();
                }
            }

            @Override
            public void onFailure(Call<ResultDO<Member>> call, Throwable t) {

            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}



